package com.bdqn.t362.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author lqm
 * @ClassName OSSUtil
 * @description: 阿里云OSS工具类
 */
public class OSSUtil {
    // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。 填写的为OSS bucket所在区域的外网Endpoint
    // 可通过
    final static   String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";
    // 填写Bucket名称，例如examplebucket。
    final static   String bucketName = "os-362";


    // RAM用户的访问密钥（AccessKey ID和AccessKey Secret）
    final static   String accessKeyId = "LTAI5t8EDGpC94jCd4GqvyhA";
    final static   String accessKeySecret = "AZpWTML4ynr8vTK6a7wbqRHYDWvNch";
    /**
     * 上传文件流到阿里云OSS
     * @param inputStream 需要上传的输入流
     * @param newFileName 保存的文件名
     * @return
     */
    public static String uploadInStream(InputStream inputStream,String newFileName){

        //声明返回字符串
        String resutlStr="";

        // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);

//        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
//        String objectName = "exampledir/exampleobject.txt";
//        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
//        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
//        String filePath= "D:\\localpath\\examplefile.txt";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, newFileName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            resutlStr= JSONObject.toJSONString(result);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return resutlStr;
    }
}
